package ru.arriah.common.ejb;

import java.rmi.*;
import javax.ejb.*;
import javax.naming.*;
import ru.arriah.common.ejb.*;

public abstract class GenericEntityBean
      extends GenericBean implements EntityBean {
   protected EntityContext entityContext;
   protected SequenceServiceLocalHome sequenceServiceHome;

   public GenericEntityBean() {
      super();
      entityContext = null;
      sequenceServiceHome = null;
   }

   protected void create(String entityName) throws CreateException {
      try {
         SequenceServiceLocalObject sequenceService;
         sequenceService = sequenceServiceHome.create();
         setId(sequenceService.getNextKeyFor(entityName));
         sequenceService.remove();
      } catch (Exception e) {
         handleException(e, "create");
         throw new EJBException(e);
      }
   }

   public void setEntityContext(
      EntityContext entityContext
   ) throws EJBException, RemoteException {
      this.entityContext = entityContext;

      try {
         sequenceServiceHome =
            (SequenceServiceLocalHome) (new InitialContext()).lookup(
               "java:comp/env/ejb/SequenceService"
            );
      } catch (Exception e) {
         handleException(e, "setEntityContext");
         throw new EJBException(e);
      }
   }

   public void unsetEntityContext() throws EJBException, RemoteException {
   }

   public void ejbRemove(
   ) throws RemoveException, EJBException, RemoteException {
   }

   public void ejbActivate() throws EJBException, RemoteException {
   }

   public void ejbPassivate() throws EJBException, RemoteException {
   }

   public void ejbLoad() throws EJBException, RemoteException {
   }

   public void ejbStore() throws EJBException, RemoteException {
   }

   public abstract Integer getId();
   public abstract void setId(Integer id);
}
